<template>
	<div class="layout-padding">
		<div class="layout-padding-auto layout-padding-view">
			<el-row shadow="hover" v-show="showSearch" class="ml10">
				<el-form :model="state.queryForm" ref="queryRef" :inline="true" @keyup.enter="getDataList">
          <el-form-item label="家长姓名" prop="nickName">
            <el-input placeholder="请输入家长姓名" v-model="state.queryForm.nickName" />
          </el-form-item>
          <el-form-item label="手机号" prop="phone">
            <el-input placeholder="请输入手机号" v-model="state.queryForm.phone" />
          </el-form-item>
					<el-form-item>
						<el-button icon="search" type="primary" @click="getDataList">
							{{ $t('common.queryBtn') }}
						</el-button>
						<el-button icon="Refresh" @click="resetQuery">{{ $t('common.resetBtn') }}</el-button>
					</el-form-item>
				</el-form>
			</el-row>
			<el-row>
				<div class="mb8" style="width: 100%">

					<right-toolbar
						v-model:showSearch="showSearch"
						class="ml10"
						style="float: right; margin-right: 20px"
						@queryTable="getDataList"
					></right-toolbar>
				</div>
			</el-row>
			<el-table
				:data="state.dataList"
				v-loading="state.loading"
				style="width: 100%"
				@selection-change="handleSelectionChange"
				border
				:cell-style="tableStyle.cellStyle"
				:header-cell-style="tableStyle.headerCellStyle"
			>
				<el-table-column type="selection" :selectable="handleSelectable" width="50" align="center" />
				<el-table-column type="index" label="#" width="80" />
        <el-table-column prop="nickName" label="家长名称" show-overflow-tooltip></el-table-column>
        <el-table-column prop="phone" label="手机号" show-overflow-tooltip></el-table-column>
				<el-table-column prop="createTime" label="创建时间" show-overflow-tooltip></el-table-column>

			</el-table>
			<pagination @size-change="sizeChangeHandle" @current-change="currentChangeHandle" v-bind="state.pagination" />
		</div>

		<!-- 角色编辑、新增  -->
		<role-dialog ref="roleDialogRef" @refresh="getDataList()" />
		<!-- 导入角色 -->
		<upload-excel
			ref="excelUploadRef"
			:title="$t('sysuser.importUserTip')"
			url="/admin/role/import"
			temp-url="/admin/sys-file/local/file/role.xlsx"
			@refreshDataList="getDataList"
		/>

	</div>
</template>

<script setup lang="ts" name="systemRole">
import { BasicTableProps, useTable } from '/src/hooks/table';
import { pageList, delObj } from '/src/api/school/parent';
import { useMessage, useMessageBox } from '/src/hooks/message';
import {listAll as listAllSchool }from '/src/api/school/school'
import {listAll as listAllGrade }from '/src/api/school/grade'
import {listAll as listAllClasses }from '/src/api/school/classes'
import { useI18n } from 'vue-i18n';

// 引入组件
const RoleDialog = defineAsyncComponent(() => import('./form.vue'));
const { t } = useI18n();

// 定义变量内容
const roleDialogRef = ref();
const permessionRef = ref();
const excelUploadRef = ref();
const queryRef = ref();
const showSearch = ref(true);
// 多选rows
const selectObjs = ref([]) as any;
// 是否可以多选
const multiple = ref(true);

const state: BasicTableProps = reactive<BasicTableProps>({
	queryForm: {
		roleName: '',
	},
	pageList: pageList, // H
	descs: ['create_time'],
});

const data = reactive({
  schoolList: [],
  gradeList:[],
  classesList:[]
})

onMounted(()=>{
  getSchoolList();
})



//  table hook
const { getDataList, currentChangeHandle, sizeChangeHandle, downBlobFile, tableStyle } = useTable(state);

// 清空搜索条件
const resetQuery = () => {
	queryRef.value.resetFields();
	getDataList();
  getSchoolList();
};


const getSchoolList = async ()=>{
  data.gradeList = [];
  data.schoolList = [];
  data.classesList = [];
  const resp = await listAllSchool();
  data.schoolList = resp.data
}

const getGradeList = async (schoolId: string)=>{
  data.gradeList = [];
  data.classesList = [];
  const resp = await listAllGrade({schoolId});
  data.gradeList = resp.data
}

const getClassesList = async (gradeId: string)=>{
  data.classesList = [];
  const resp = await listAllClasses({gradeId});
  data.classesList = resp.data
}


// 导出excel
const exportExcel = () => {
	downBlobFile('/admin/role/export', state.queryForm, 'role.xlsx');
};

// 是否可以多选
const handleSelectable = (row: any) => {
	return row.roleId !== '1';
};

// 多选事件
const handleSelectionChange = (objs: { id: string }[]) => {
	selectObjs.value = objs.map(({ id }) => id);
	multiple.value = !objs.length;
};

// 删除操作
const handleDelete = async (ids: string[]) => {
	try {
		await useMessageBox().confirm(t('common.delConfirmText'));
	} catch {
		return;
	}

	try {
		await delObj(ids);
		getDataList();
		useMessage().success(t('common.delSuccessText'));
	} catch (err: any) {
		useMessage().error(err.msg);
	}
};
</script>
